package com.kaibes.web.thymeleaf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.kaibes.core.base.util.ClassUtils;
import com.kaibes.core.base.util.LocalDateTimeUtils;
import com.kaibes.core.base.util.LocalDateUtils;
import com.kaibes.core.base.util.StringUtils;

@Configuration
public class ThymeleafConfig implements WebMvcConfigurer {

    @Autowired
    private CacheHandlerInterceptor cacheHandlerInterceptor;
//    @Autowired
//    private AutoTemplateResolver resolver;
    
    @Value("${spring.thymeleaf.cache:true}")
    private boolean cache;
    @Value("${spring.thymeleaf.check-template-location:false}")
    private boolean check;
    @Value("${spring.thymeleaf.template-resolver-order:1}")
    private int order;
    @Value("${spring.thymeleaf.prefix:templates/}")
    private String prefix;
    @Value("${spring.thymeleaf.suffix:.html}")
    private String suffix;
    @Value("${spring.thymeleaf.mode:HTML}")
    private String mode;
    
    private Map<String, Object> staticVariables = new HashMap<>();
	{
		staticVariables.put("StringUtils", StringUtils.class);
		staticVariables.put("ClassUtils", ClassUtils.class);
		staticVariables.put("LocalDateTimeUtils", LocalDateTimeUtils.class);
		staticVariables.put("LocalDateUtils", LocalDateUtils.class);
//        staticVariables.put("StringUtils", new StringUtils());
//        staticVariables.put("ClassUtils", new ClassUtils());
//        staticVariables.put("LocalDateTimeUtils", new LocalDateTimeUtils());
//        staticVariables.put("LocalDateUtils", new LocalDateUtils());
	}
	
    private Map<String, Object> baseModel;
    private List<Variable> baseVariables;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(cacheHandlerInterceptor);
    }
    
    @PostConstruct
    public void init() {
        baseModel = new HashMap<>();
        baseVariables = new ArrayList<>();
        // dateTime
        Variable variable = new Variable();
        variable.setName("当前时间");
        variable.setValue("dateTime1");
        baseVariables.add(variable);
        variable = new Variable();
        variable.setName("当前时间(中文)");
        variable.setValue("dateTime2");
        baseVariables.add(variable);
    }
    
    public Map<String, Object> getStaticVariables() {
		return staticVariables;
	}
	
	public void putStaticVariable(Class<?> clazz) {
		staticVariables.put(clazz.getSimpleName(), clazz);
	}

	public void putStaticVariable(String key, Object value) {
		staticVariables.put(key, value);
	}
    
    public Map<String, Object> getBaseModel() {
        return baseModel;
    }
    
    public List<Variable> getBaseVariables() {
        return baseVariables;
    }
    
}
